//
//  MessageCase.m
//  wopu
//
//  Created by meeidol on 2016/11/22.
//  Copyright © 2016年 zhangxiang. All rights reserved.
//

#import "ZXMessageCase.h"
#import "NSString+Size.h"

#define SCREEN_Width [UIScreen mainScreen].bounds.size.width
#define SCREEN_Height [UIScreen mainScreen].bounds.size.height
#define xsIZE [UIScreen mainScreen].bounds.size.width/375
#define ysIZE [UIScreen mainScreen].bounds.size.height/667

@interface ZXMessageCase (){
    CGSize LabelSize;
}
@property (nonatomic,strong) UIView *showview;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIView *bg;
@property (nonatomic,strong) UILabel *progressLabel;
@end

@implementation ZXMessageCase

+(ZXMessageCase *)shareInstace{
    static ZXMessageCase *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[ZXMessageCase alloc] init];
    });
    return share;
}

-(void)addMember:(id)object{
    
    if(object){
        [_groupArray addObject:object];
    }
    
}

-(NSMutableArray *)groupArray{
    if(!_groupArray){
        _groupArray = [NSMutableArray new];
    }
    
    return _groupArray;
}

- (void)showMessage:(NSString *)message{
    
    self.showview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
    self.showview.layer.masksToBounds = YES;
     [[UIApplication sharedApplication].keyWindow addSubview:self.bg];
    [self.bg addSubview:self.showview];

    LabelSize = [NSString textString:message fontSize:15 maxWidth:CGFLOAT_MAX];
    self.label.frame = CGRectMake(15 * xsIZE, (45 * ysIZE -  LabelSize.height)/ 2, LabelSize.width, LabelSize.height);
    self.label.text = message;
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = 1;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.font = [UIFont systemFontOfSize:15];
    [self.showview addSubview:self.label];
    
    self.showview.frame = CGRectMake(0, 0, LabelSize.width+ 30 * xsIZE, 45 * ysIZE);
    self.showview.center = CGPointMake(SCREEN_Width /2, SCREEN_Height /2);
//    self.showview.layer.cornerRadius = self.showview.frame.size.height / 2.0;
    self.showview.layer.cornerRadius = 5;
    
//    [UIView animateWithDuration:2 animations:^{
//
//    } completion:^(BOOL finished) {
//        [self.bg removeFromSuperview];
//        [self.label removeFromSuperview];
//        [self.showview removeFromSuperview];
//    }];
    [NSTimer scheduledTimerWithTimeInterval:1.5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self.bg removeFromSuperview];
        [self.label removeFromSuperview];
        [self.showview removeFromSuperview];
    }];
}


/// 更改背景颜色
/// - Parameters:
///   - message: 提示的内容
///   - color: 背景颜色修改
- (void)showMessage:(NSString *)message withColor:(UIColor *)color{
    
    self.showview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
    self.showview.layer.masksToBounds = YES;
     [[UIApplication sharedApplication].keyWindow addSubview:self.bg];
    [self.bg addSubview:self.showview];

    LabelSize = [NSString textString:message fontSize:15 maxWidth:CGFLOAT_MAX];
    self.label.frame = CGRectMake(15 * xsIZE, (40 * ysIZE -  LabelSize.height)/ 2, LabelSize.width, LabelSize.height);
    self.label.text = message;
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = 1;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.font = [UIFont systemFontOfSize:15];
    [self.showview addSubview:self.label];
    
    self.showview.frame = CGRectMake(0, 0, LabelSize.width+ 30 * xsIZE, 40 * ysIZE);
    self.showview.center = CGPointMake(SCREEN_Width /2, SCREEN_Height /2);
//    self.showview.layer.cornerRadius = self.showview.frame.size.height / 2.0;
    self.showview.layer.cornerRadius = 40 * ysIZE/2;
    
//    [UIView animateWithDuration:2 animations:^{
//
//    } completion:^(BOOL finished) {
//        [self.bg removeFromSuperview];
//        [self.label removeFromSuperview];
//        [self.showview removeFromSuperview];
//    }];
    [NSTimer scheduledTimerWithTimeInterval:1.5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [self.bg removeFromSuperview];
        [self.label removeFromSuperview];
        [self.showview removeFromSuperview];
    }];
}



-(void)showMessage:(NSString *)message time:(CGFloat)time{
  
    self.showview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
    self.showview.layer.masksToBounds = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:self.bg];
    [[UIApplication sharedApplication].keyWindow addSubview:self.showview];
    LabelSize = [NSString textString:message fontSize:15 maxWidth:CGFLOAT_MAX];

    self.label.frame = CGRectMake(15 * xsIZE, (45 * ysIZE -  LabelSize.height)/ 2, LabelSize.width, LabelSize.height);
    self.label.text = message;
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = 1;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.font = [UIFont systemFontOfSize:15];
    [self.showview addSubview:self.label];
    
    self.showview.frame = CGRectMake(0, 0, LabelSize.width+ 30 * xsIZE, 45 * ysIZE);
    self.showview.center = CGPointMake(SCREEN_Width /2, SCREEN_Height /2);
    //    self.showview.layer.cornerRadius = self.showview.frame.size.height / 2.0;
    self.showview.layer.cornerRadius = 5;
    
    [UIView animateWithDuration:time animations:^{
        self.showview.alpha = 0;
    } completion:^(BOOL finished) {
        [self.bg removeFromSuperview];
        [self.showview removeFromSuperview];
        [self.label removeFromSuperview];
        self.showview.alpha = 1;
    }];
}

-(void)showMessage:(NSString *)message time:(CGFloat)time completion:(void (^)(void))completion{
    self.bg.backgroundColor = [UIColor clearColor];
    
    self.showview.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
    self.showview.layer.masksToBounds = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:self.bg];
    [[UIApplication sharedApplication].keyWindow addSubview:self.showview];
    
    LabelSize = [NSString textString:message fontSize:15 maxWidth:CGFLOAT_MAX];
    self.label.frame = CGRectMake(15 * xsIZE, (45 * ysIZE -  LabelSize.height)/ 2, LabelSize.width, LabelSize.height);
    self.label.text = message;
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = 1;
    self.label.backgroundColor = [UIColor clearColor];
    self.label.font = [UIFont systemFontOfSize:15];
    
    [self.showview addSubview:self.label];
    self.showview.frame = CGRectMake(0, 0, LabelSize.width+ 30 * xsIZE, 45 * ysIZE);
    self.showview.center = CGPointMake(SCREEN_Width /2, SCREEN_Height /2);
    //    self.showview.layer.cornerRadius = self.showview.frame.size.height / 2.0;
    self.showview.layer.cornerRadius = 5;
    
    
    [UIView animateWithDuration:time animations:^{
        self.showview.alpha = 0;
    } completion:^(BOOL finished) {
        [self.bg removeFromSuperview];
        [self.showview removeFromSuperview];
        [self.label removeFromSuperview];
        self.showview.alpha = 1;
        if (completion) {
            completion();
        }
    }];
    
}

-(void)showCallView:(int)time{
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [self.callImageView addGestureRecognizer:gesture];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.callImageView addGestureRecognizer:tap];
    
    [window addSubview:self.callImageView];
}

-(void)disMissCallView{
    [self.callImageView removeFromSuperview];
    self.callVC = nil;
}


-(UIView *)showview{
    if (!_showview) {
        _showview = [[UIView alloc]init];
    }
    return _showview;
}
-(UILabel *)label{
    if (!_label) {
        _label = [[UILabel alloc]init];
    }
    return _label;
}
-(UIView *)bg{
    if (!_bg) {
        _bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _bg.backgroundColor = [UIColor clearColor];
    }
    return _bg;
}

-(UIImageView *)callImageView{
    if(!_callImageView){
        _callImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"拨打中"]];
        _callImageView.frame = CGRectMake(SCREEN_WIDTH-52, 200, 52, 52);
        _callImageView.userInteractionEnabled = YES;
        [_callImageView addSubview:self.callLabel];
    }
    return _callImageView;
}




-(void)panGesture:(UIPanGestureRecognizer *)gesture{
    if(gesture.state == UIGestureRecognizerStateChanged){
        CGPoint offset = [gesture translationInView:[UIApplication sharedApplication].keyWindow];
        
        
        if(_callImageView.center.x+offset.x < 25 || _callImageView.center.x+offset.x > SCREEN_WIDTH-25 || _callImageView.center.y+offset.y<60 || _callImageView.center.y+offset.y > SCREEN_HEIGHT-60 ){
            
        }else{
            _callImageView.center = CGPointMake(_callImageView.center.x+offset.x, _callImageView.center.y+offset.y);
        }
        
        
        // 复位,表示相对上一次
        [gesture setTranslation:CGPointMake(0, 0) inView:[UIApplication sharedApplication].keyWindow];
    }
    
}

-(void)tapGesture:(UITapGestureRecognizer *)tap{
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.callVC animated:YES completion:nil];
    [_callImageView removeFromSuperview];   
    
}

-(void)showHud:(NSString *)message{
    dispatch_after(0, dispatch_get_main_queue(), ^(void){
        
        UIWindow *window =  [UIApplication sharedApplication].keyWindow;
        if(self.hud){
            [MBProgressHUD hideHUDForView:window animated:NO];
            self.hud = nil;
        }
        self.hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        self.hud.mode = MBProgressHUDModeIndeterminate;
        self.hud.label.text = message;
    });
    
    
}

-(void)hideHud{
    
    dispatch_after(0.2, dispatch_get_main_queue(), ^(void){
        UIWindow *window =  [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD hideHUDForView:window animated:YES];
        self.hud = nil;
    });
}

@end
